//
//  ViewController.m
//  23-QQ音乐
//
//  Created by dllo on 16/8/15.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"
#import "AFNetworking.h"
#import "RootModel.h"
#import "MvList.h"
#import "List.h"
#import "Contents.h"
#import "Data.h"
#import "MVTableViewCell.h"
#define KTableViewCellResult @"cell"
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *qqMVTableView;
@property (nonatomic, strong)NSMutableArray *headerArr;
@property (nonatomic, strong)RootModel *rootModel;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self handleData];
    
}
- (void)handleData {
   
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:@"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&format=jsonp&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294&jsonpCallback=jsonp1" parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSData *data = responseObject;
        NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
        [dataString stringByReplacingOccurrencesOfString:@"jsonp1(" withString:@""];
       dataString = [dataString substringToIndex:dataString.length - 1];
        NSLog(@"%@", dataString);
        NSData *finadata= [dataString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error = nil;
        id result = [NSJSONSerialization JSONObjectWithData:finadata options:NSJSONReadingMutableContainers error:&error];
        self.rootModel = [RootModel modelWithDic:result];
        [_qqMVTableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.rootModel.data.contents.list.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_rootModel.data.contents.list[section] mvlist].count - 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MVTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:KTableViewCellResult];
    cell.mvlist = [_rootModel.data.contents.list[indexPath.section] mvlist][indexPath.row + 1];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
